package Bean;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.shape.Line;

import java.io.Serializable;

public class MindMapLine extends Line implements Serializable {

    private  transient SimpleDoubleProperty beginX = new SimpleDoubleProperty();

    private  transient SimpleDoubleProperty beginY = new SimpleDoubleProperty();

    private  transient SimpleDoubleProperty endX = new SimpleDoubleProperty();

    private  transient SimpleDoubleProperty endY = new SimpleDoubleProperty();

    private double MBeginX;

    private double MBeginY;

    private double MEndX;

    private double MEndY;

    private MindMapNode beginNode;

    private MindMapNode endNode;

    public MindMapLine(){}

    public MindMapLine(MindMapNode beginNode,MindMapNode endNode,double beginX,double beginY,double endX,double endY){
        init(beginNode, endNode, beginX, beginY, endX, endY);
    }

    public void init(MindMapNode beginNode,MindMapNode endNode,double beginX,double beginY,double endX,double endY){
        super.setStartX(beginX);
        super.setStartY(beginY);
        super.setEndX(endX);
        super.setEndY(endY);
        this.beginNode = beginNode;
        this.endNode = endNode;
        if(this.beginX==null){
            this.beginX = new SimpleDoubleProperty();
        }
        if(this.beginY==null){
            this.beginY = new SimpleDoubleProperty();
        }
        if(this.endX==null){
            this.endX = new SimpleDoubleProperty();
        }
        if(this.endY==null){
            this.endY = new SimpleDoubleProperty();
        }
        this.beginX.bind(beginNode.xProperty());
        this.beginY.bind(beginNode.yProperty());
        this.endX.bind(endNode.xProperty());
        this.endY.bind(endNode.yProperty());
        this.beginX.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                MindMapLine.super.setStartX(newValue.doubleValue()+MindMapLine.this.beginNode.getPrefWidth()/2);
            }
        });
        this.beginY.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                MindMapLine.super.setStartY(newValue.doubleValue()+MindMapLine.this.beginNode.getPrefHeight()/2);
            }
        });
        this.endX.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                MindMapLine.super.setEndX(newValue.doubleValue()+MindMapLine.this.endNode.getPrefWidth()/2);
            }
        });
        this.endY.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                MindMapLine.super.setEndY(newValue.doubleValue()+MindMapLine.this.endNode.getPrefHeight()/2);
            }
        });
    }

    public double getBeginX() {
        return beginX.get();
    }


    public void setBeginX(double beginX) {
        if(this.beginX==null){
            this.beginX = new SimpleDoubleProperty();
        }
        this.beginX.set(beginX);
    }

    public double getBeginY() {
        return beginY.get();
    }


    public void setBeginY(double beginY) {
        if(this.beginY==null){
            this.beginY = new SimpleDoubleProperty();
        }
        this.beginY.set(beginY);
    }

    public double getEndOfX() {
        return endX.get();
    }


    public void setEndOfX(double endX) {
        if(this.endX==null){
            this.endX = new SimpleDoubleProperty();
        }
        this.endX.set(endX);
    }


    public double getEndOfY() {
        return endY.get();
    }


    public void setEndOfY(double endY) {
        if(this.endY==null){
            this.endY = new SimpleDoubleProperty();
        }
        this.endY.set(endY);
    }

    public double getMBeginX() {
        return MBeginX;
    }

    public void setMBeginX(double MBeginX) {
        this.MBeginX = MBeginX;
    }

    public double getMBeginY() {
        return MBeginY;
    }

    public void setMBeginY(double MBeginY) {
        this.MBeginY = MBeginY;
    }

    public double getMEndX() {
        return MEndX;
    }

    public void setMEndX(double MEndX) {
        this.MEndX = MEndX;
    }

    public double getMEndY() {
        return MEndY;
    }

    public void setMEndY(double MEndY) {
        this.MEndY = MEndY;
    }

    public MindMapNode getBeginNode() {
        return beginNode;
    }

    public void setBeginNode(MindMapNode beginNode) {
        this.beginNode = beginNode;
    }

    public MindMapNode getEndNode() {
        return endNode;
    }

    public void setEndNode(MindMapNode endNode) {
        this.endNode = endNode;
    }

}
